package cn.e2win.biz.comm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.WeixinUser;

public class WeiXinInterceptor extends HandlerInterceptorAdapter{
	public WeiXinInterceptor(){
		super();
		System.err.println("========WeiXinInterceptor()");
	}
	private static final String[] IGNORE_URL = {"/m/login.do", "/m/main.do"};
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag = false;
        String url = request.getRequestURL().toString();
        //不拦截上面定义的路径
        for (String str : IGNORE_URL) {
            if (url.contains(str)) {
                System.err.println("不需要拦截======" + url);
                flag = true;
                break;
            }
        }
        if (!flag) {
        	System.err.print("需要拦截======" + url);
            SysUser user = (SysUser)request.getSession().getAttribute("user");
            WeixinUser wxUser = (WeixinUser)request.getSession().getAttribute("weixinUser");
            if (user != null){
            	System.err.print("=====user=======" + user.getAccount());
            	flag = true;
            }else{
            	String redirect = "../m/login.do";
            	if(wxUser!=null){
            		redirect = "../m/main.do";
            	}
        		response.sendRedirect(redirect);
                System.err.println("=====跳转=====" + redirect);
            	return false;
            }
        }
        return true;
    }
	    
    @Override
    public void postHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    	
    }
	    
    @Override
    public void afterCompletion(HttpServletRequest request,
        HttpServletResponse response, Object handler, Exception ex)
        throws Exception {
    	
    }

}
